// test_logic.h
#ifndef __LOGIC_TEST_H
#define __LOGIC_TEST_H

#include "common.h"

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/config/SourcePrefix.h>

class LogicTest : public CPPUNIT_NS::TestFixture
{
	CPPUNIT_TEST_SUITE( LogicTest );
	CPPUNIT_TEST( test_dlx_solver );
    CPPUNIT_TEST( test_generate_sudoku );
    CPPUNIT_TEST( test_Solver );
    CPPUNIT_TEST( test_Nothing );
	CPPUNIT_TEST_SUITE_END();

public:
	void setUp(){}
	void tearDown(){}

	void test_dlx_solver()
	{
		cout << "test dlx_solver ..." << endl;
		dlx_solver ds;
		map<string,string> data;
		/*
		data["+---+---+---+\n"
		"|4..|...|..5|\n"
		"|.2.|...|.6.|\n"
		"|..9|...|1..|\n"
		"+---+---+---+\n"
		"|8..|6.5|..4|\n"
		"|.6.|.7.|.2.|\n"
		"|1..|3.4|..9|\n"
		"+---+---+---+\n"
		"|..1|...|3..|\n"
		"|.5.|...|.8.|\n"
		"|7..|...|..1|\n"
		"+---+---+---+\n"]="417862935325917468689543172893625714564179823172384659241758396956431287738296541";
		*/
		data["4.......5.2.....6...9...1..8..6.5..4.6..7..2.1..3.4..9..1...3...5.....8.7.......1"]="417862935325917468689543172893625714564179823172384659241758396956431287738296541";
		data["400000005020000060009000100800605004060070020100304009001000300050000080700000001"]="417862935325917468689543172893625714564179823172384659241758396956431287738296541";
		for(map<string,string>::const_iterator itor=data.begin();itor!=data.end();++itor)
		{
			string str=ds.do_solve(itor->first) ;
			cout<< endl << itor->first <<endl <<  str << endl;
			CPPUNIT_ASSERT(str == itor->second);
		}
	}

	void test_generate_sudoku()
	{
		cout << "test generate sudoku string ..." << endl;
		generator gen;
		dlx_solver ds;
		//for(int i=0;i<10;++i)
		//{
		//	string str=gen.do_dlx_generator();
		//	CPPUNIT_ASSERT(ds.solution_count(str)==1);
		//}
	}


	void test_Solver()
	{
        cout << "test sudoku solver ..." << endl;
		string strResult="417862935325917468689543172893625714564179823172384659241758396956431287738296541";
		Puzzle pz;
		string str2="417862935325917468689543172893625714564179823172384659241758396956431287738296540";
		pz.loadPuzzle(str2.c_str());
        CPPUNIT_ASSERT(pz.getStatus()==PuzzleIsPlaying);
        HelperSingleNumber helperSingleNumber;
        Solver solver;
        solver.registerHelper(&helperSingleNumber); //验证唯一数解法
        Operations ops;
        solver.doSolve(&pz,ops);
        string tmp=pz.exportPuzzle();
        //cout << endl << tmp << endl << "vs" << endl << strResult<<endl;
        CPPUNIT_ASSERT(stricmp(tmp.c_str(), strResult.c_str())==0);
        string strSingleNumber="706308409809260305352400680567901832480502006903806040294005173135724008678103254";
        string strSingleNumberResult="716358429849267315352419687567941832481532796923876541294685173135724968678193254";
        pz.loadPuzzle(strSingleNumber.c_str());
        cout<<endl<<pz.exportPuzzle(FormatFullTable)<<endl;
        solver.doSolve(&pz,ops);
        cout << endl << "Dump Solve steps!" << endl;
        for(unsigned int i=0;i<ops.size();++i)
        {
            cout<< ops[i].toString()<<endl;
        }
        cout<<pz.exportPuzzle(FormatFullTable)<<endl;
        CPPUNIT_ASSERT(pz.getStatus()==PuzzleIsOver);

        pz.loadPuzzle(strSingleNumber.c_str());
        Solver solver_isr;
        HelperIntersectionRemoval helperIntersectionRemoval;
        solver_isr.registerHelper(&helperIntersectionRemoval);
	}

    void test_Nothing()
    {
        cout << "test Nothing !!!" << endl;
    }
};
CPPUNIT_TEST_SUITE_REGISTRATION( LogicTest );


#endif  // __LOGIC_TEST_H
